package com.cskaoyan.controller.wx.service;

import com.cskaoyan.bean.entity.Address;
import com.cskaoyan.bean.entity.AddressExample;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther Wenjian_Yang
 * @Date 2021/10/22
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper addressMapper;

    @Override
    public BasePageData<Address> query() {
        AddressExample addressExample = new AddressExample();
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        return BasePageData.ok(addresses);
    }

    @Override
    public Address detail(Integer id) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (id != null && !id.equals("")){
            criteria.andIdEqualTo(id);
        }
        Address address = addressMapper.selectByPrimaryKey(id);

        return address;
    }


    @Override
    public int save(Address address) {

        if (address.getId() != 0){
            addressMapper.updateByPrimaryKeySelective(address);
        }else {
            addressMapper.insertSelective(address);
        }
        return address.getId();
    }

    @Override
    public int delete(Address address) {
        int i = addressMapper.deleteByPrimaryKey(address.getId());
        return i;
    }
}
